﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_OrderTour : System.Web.UI.Page
{
    public static int orderID;
    public static bool isCheck;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!checkLoginAdmin())
            {
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('You have to login as Admin to access this page!');</script>");
                Response.Redirect("Login.aspx?prePage=orderTour");
            }
            LoadData();

            //Sort
            if (GridView1.Rows.Count > 0)
            {
                GridView1.UseAccessibleHeader = true;
                GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
                //GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            }
            else
            {
                isCheck = true;
                lblCalRoom.Visible = false;
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('Have not order tour!');</script>");
            }
        }
    }
    // check login
    public bool checkLoginAdmin()
    {
        // check session
        if (Session["username"] != null)
        {
            return true;
        }
        else
        {
            // check cookie 
            if (Request.Cookies["username"] != null)
            {
                Session["username"] = Request.Cookies["username"].Value;
                Session["fullname"] = Request.Cookies["fullname"].Value;
                return true;
            }

        }
        return false;
    }
    private void LoadData()
    {
        DataTable dt = OrderTourManagement.GetOrders().Tables["AllOrderTour"];
        DataColumn newColumn = new DataColumn("numRoom");
        dt.Columns.Add(newColumn);
        foreach (DataRow row in dt.Rows)
        {
            double adult = double.Parse(row["numAdult"].ToString());
            double child = double.Parse(row["numChild"].ToString());
            double room = Math.Ceiling(adult / Common.ADULT_PER_ROOM + child / Common.CHILD_PER_ROOM);
            row["numRoom"] = room;
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        orderID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        if (OrderTourManagement.DeleteOrderTour(orderID))
            LoadData();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        orderID = int.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
        DataTable dtTour = OrderTourManagement.GetOrderByID(orderID);
        if (dtTour.Rows.Count > 0)
        {
            lblCusName.Text = dtTour.Rows[0][CusManagement.CUSNAME].ToString();
            ddlAdult.SelectedValue = dtTour.Rows[0][OrderTourManagement.NUMADULT].ToString();
            ddlChild.SelectedValue = dtTour.Rows[0][OrderTourManagement.NUMCHILD].ToString();
            lblTel.Text = dtTour.Rows[0][CusManagement.TEL].ToString();
            lblEmail.Text = dtTour.Rows[0][CusManagement.EMAIL].ToString();
            lblTourCode.Text = dtTour.Rows[0][TourManagement.TOURCODE].ToString();
            lblTourName.Text = dtTour.Rows[0][TourManagement.TOURNAME].ToString();
            lblPlaceStart.Text = dtTour.Rows[0][TourManagement.PLACESTART].ToString();
            lblPlaceEnd.Text = dtTour.Rows[0][TourManagement.PLACEEND].ToString();
            lblDateStart.Text = DateTime.Parse(dtTour.Rows[0][TourManagement.STARTDATE].ToString()).ToShortDateString();
            lblDateEnd.Text = DateTime.Parse(dtTour.Rows[0][TourManagement.ENDDATE].ToString()).ToShortDateString();
            lblItinerary.Text = dtTour.Rows[0][TourManagement.ITINERARY].ToString();
            lblPlan.Text = dtTour.Rows[0][TourManagement.PLANTOUR].ToString();
        }

        MultiView1.ActiveViewIndex = 1;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        isCheck = true;
        orderID = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        if (OrderTourManagement.EditOrderTour(orderID, 1))
        {
            ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('This tour is checked!');</script>");
            LoadData();
        }
        e.Cancel = true;
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        DataTable dt;
        if (isCheck)
        {
            dt = OrderTourManagement.GetAllOrder().Tables[0];
            DataColumn newColumn = new DataColumn("numRoom");
            dt.Columns.Add(newColumn);
            foreach (DataRow row in dt.Rows)
            {
                double adult = double.Parse(row["numAdult"].ToString());
                double child = double.Parse(row["numChild"].ToString());
                double room = Math.Ceiling(adult / Common.ADULT_PER_ROOM + child / Common.CHILD_PER_ROOM);
                row["numRoom"] = room;
            }

            LinkButton3.Text = "Hidden tours checked!";
            isCheck = false;
            lblCalRoom.Visible = true;
        }
        else
        {
            dt = OrderTourManagement.GetOrders().Tables[0];
            DataColumn newColumn = new DataColumn("numRoom");
            dt.Columns.Add(newColumn);
            foreach (DataRow row in dt.Rows)
            {
                double adult = double.Parse(row["numAdult"].ToString());
                double child = double.Parse(row["numChild"].ToString());
                double room = Math.Ceiling(adult / Common.ADULT_PER_ROOM + child / Common.CHILD_PER_ROOM);
                row["numRoom"] = room;
            }

            LinkButton3.Text = "Display all tours";
            isCheck = true;
            lblCalRoom.Visible = false;
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
    }
}